 //
//  InternetBrowserViewController.m
//  Lightspeed Browser
//
//  Created by iD Student on 7/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "InternetBrowserViewController.h"
#import "PageViewController.h"
#import <QuartzCore/QuartzCore.h>

@class PageViewController;

@interface InternetBrowserViewController ()

@end

@implementation InternetBrowserViewController
@synthesize settings;
@synthesize page;
@synthesize webView;

-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                              message:@"Failed to Connect. Please Check Your Internet Connection" 
                                              delegate:self
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
}

- (IBAction)dismissKeyboard:(id)sender {
    
//    [sender resignFirstResponder];
//    CGRect new_frame=CGRectMake(googleBar.frame.origin.x+50, googleBar.frame.origin.y, googleBar.frame.size.width-50, googleBar.frame.size.height);
//    [googleBar setFrame:new_frame];
}

-(IBAction)refresh:(id)sender
{
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.yahoo.com"]]];
}

-(IBAction)search:(id)sender
{
    NSString *query = [searchBar.text stringByReplacingOccurrencesOfString:@" " withString: @"+"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", query]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

-(IBAction)google:(id)sender
{
    //CGRect new_frame=CGRectMake(googleBar.frame.origin.x-50, googleBar.frame.origin.y, googleBar.frame.size.width+50, googleBar.frame.size.height);
    //[googleBar setFrame:new_frame];
    
    NSString *query = [googleBar.text stringByReplacingOccurrencesOfString:@" " withString: @"+"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?q=%@", query]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];

}


-(IBAction)home:(id)sender
{
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
}


- (void)viewDidLoad
{
    
    //[self refresh:self];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(webViewLoading) userInfo:nil repeats:YES];
    activity.backgroundColor = [UIColor lightGrayColor];
    activity.hidesWhenStopped = YES;
    
    
    [super viewDidLoad];

	// Do any additional setup after loading the view, typically from a nib.
}


-(void)webViewLoading
{
    if(!webView.loading)
    {
        [activity stopAnimating];
    }
    else 
    {
        [activity startAnimating];
    }
            
}



- (void)viewDidUnload
{
    [self setSettings:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

-(IBAction)addPage:(id)sender
{
    page = [self.storyboard instantiateViewControllerWithIdentifier:@"pageView"];
    page.internet=self;
    [self presentModalViewController: page animated:TRUE];
    ;
    [webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    

    

}


-(IBAction)settings:(id)sender
{
    settings= [self.storyboard instantiateViewControllerWithIdentifier:@"settingsView"];
    [self presentModalViewController: settings animated:TRUE];
}




@end
